package cn.rc.rcsystem.web.controller;


import cn.rc.rcsystem.domain.CarRentals;
import cn.rc.rcsystem.result.PageList;
import cn.rc.rcsystem.service.ICarRentalsService;
import com.baomidou.mybatisplus.plugins.Page;
import cn.rc.rcsystem.query.CarRentalsQuery;
import cn.rc.rcsystem.result.JSONResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/carRental")
public class CarRentalsController {

    @Autowired
    public ICarRentalsService carRentalsService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CarRentals carRentals){
        if(carRentals.getId()!=null){
            carRentalsService.updateById(carRentals);
        }else{
            carRentalsService.insert(carRentals);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        carRentalsService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(carRentalsService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(carRentalsService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CarRentalsQuery query){
        Page<CarRentals> page = new Page<CarRentals>(query.getPage(),query.getRows());
        page = carRentalsService.selectPage(page);
        return JSONResult.success(new PageList<CarRentals>(page.getTotal(),page.getRecords()));
    }

    /**
     * 添加上线车辆
     * @param carRental
     * @return
     */
    @RequestMapping(value = "/cargrounding",method = RequestMethod.POST)
    public JSONResult cargrounding(@RequestBody CarRentals carRental){
        return carRentalsService.cargrounding(carRental);
    }
    /**
     *  租车
     */
    @RequestMapping(value = "/rent",method = RequestMethod.POST)
    public JSONResult rent(@RequestBody CarRentals carRental){
        return carRentalsService.rent(carRental);
    }

}
